
import com.renesys.raceway.views.*;

import java.awt.*;

public class impulsePlayer extends bgpStreamPlayer {
    public static long MIN_DURATION = 100; // ms
    public Color idleColor = new Color(160,176,255);

    private void pulse(Raceway.VisibleHost h) {
	long ms0 = System.currentTimeMillis();
	while (System.currentTimeMillis()-ms0 < MIN_DURATION) {
	    h.scaleSize(1.25);
	    h.scaleSize(0.8);
	}
    }
    /** The following sends a red pulse down the link, if visible, 
     *  in 10 frames.  Then it returns in the other direction in 
     *  blue, in 10 more frames.  Just for fun -- no meaning. 
     */
    private void pulse(Raceway.VisibleLink tL) {
	final int frames = 10;
	final Raceway.VisibleLink L = tL;
	if (null!=L) 
	    (new Thread() {
		    public void run() {
			Object impulse = null;
			for (double d = 0.0; d<=1.0; d+=1./frames) {
			    impulse = L.pulse(impulse,
					      d,
					      d+0.05,
					      3,
					      3,
					      L.LEFT,
					      Color.red);
			}
			impulse = L.pulse(impulse,0,0,0,0,0,null); // kill it
			for (double d = 1.0; d>0.0; d-=1./frames) {
			    impulse = L.pulse(impulse,
					      d,
					      d+0.05,
					      3,
					      3,
					      L.RIGHT,
					      Color.blue);
			}
			impulse = L.pulse(impulse,0,0,0,0,0,null); // kill it
		    }
		}).start();
    }

    public void play(Stream.Record r) {
	String rty = 
	    stream.getRecordTypeString(r.type_code);
	String nhi = 
	    stream.getRecordSourceString(r.source_code);
	if (rty.equals("SSF.OS.BGP4")) {
	    switch((int)r.bytes[r.offset]) {
	    case SSF.OS.BGP4.Monitor.CPU_BUSY:
		Raceway.VisibleHost h = stream.raceway().getVisibleHost(nhi);

		// Just guessing that a link by the following 
		// name exists.. for demo purposes only, has 
		// no meaning:
		//
		pulse(stream.raceway().getVisibleLink(nhi+"(0)"));
		//
		    
		if (null != h) {
		    if (0==r.bytes[r.offset+1]){
			h.setFillColor(idleColor);  // IDLE
		    } else {
			h.setFillColor(Color.red);  // ACTIVE 
			pulse(h);
		    }
		} 
	    default:
				// ignore other records 
		break;
	    }
	}
    }
}
